package cn.skyquant.quant4j.task.boot.task;

import cn.skyquant.quant4j.api.dto.ResultDTO;
import cn.skyquant.quant4j.api.enums.BorkerType;
import cn.skyquant.quant4j.api.goods.GoodsDTO;
import cn.skyquant.quant4j.api.goods.GoodsService;
import cn.skyquant.quant4j.api.goods.GoodsTickDTO;
import cn.skyquant.quant4j.api.quant.Tick;
import cn.skyquant.quant4j.api.quant.jforex.JForexService;
import cn.skyquant.quant4j.sdk.enums.Environment;
import cn.skyquant.quant4j.sdk.util.maths.MathsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.List;

@RestController()
@RequestMapping("/goods")
@Component
public class GoodsTask {
    @Autowired
    GoodsService goodsService;

    @Autowired
    JForexService jForexService;

    @Value("${runtime.environment}")
    Environment environment;

    @Value("${task.run}")
    boolean taskrun;

    @PostConstruct
    public void init(){
        if(environment==Environment.product){
            updateGoodsForDukascopy();
        }
    }

    //每周1-5的8-23点
    @Scheduled(cron = "0 0 8-23 ? * 1-5")
    @RequestMapping(value = "/spread/dukascopy", method = RequestMethod.GET)
    public void updateGoodsForDukascopy() {
        if(taskrun){
            ResultDTO<List<GoodsDTO>> goodsResult = goodsService.findAllGoods(BorkerType.DUKASCOPY);
            if (goodsResult.okData()) {
                List<GoodsDTO> goodsDTOList = goodsResult.data;
                goodsDTOList.forEach(x -> updateTickByGoods(x));
            }
        }
    }

    private static int Max_GoodsTick_Size = 3;

    public void updateTickByGoods(GoodsDTO goodsDTO) {
        ResultDTO<List<GoodsTickDTO>> goodsTickResult = goodsService.findGoodsTickByGoodsId(goodsDTO.id);
        double sum_max_spread = 0;
        int count_size = 0;
        if (goodsTickResult.okData() ) {
            List<GoodsTickDTO> goodsTickDTOList = goodsTickResult.data;
            for (int i = 0;i<goodsTickDTOList.size();i++) {
                GoodsTickDTO goodsTickDTO = goodsTickDTOList.get(i);
                if(i<Max_GoodsTick_Size-1 && goodsTickDTO.ask!=null && goodsTickDTO.bid!=null){
                    sum_max_spread += goodsTickDTO.ask - goodsTickDTO.bid;
                    count_size ++;
                }else{
                    goodsService.deleteGoodsTickById(goodsTickDTO.id);
                }
            }
        }
        ResultDTO<Tick> resultDTO = jForexService.getLastTick(goodsDTO.symbol);
        if (resultDTO.okData()) {
            Tick tick = resultDTO.data;
            GoodsTickDTO newGoodsTick = new GoodsTickDTO();
            newGoodsTick.ask = tick.ask;
            newGoodsTick.bid = tick.bid;
            newGoodsTick.askVolumn = tick.askVolumn;
            newGoodsTick.bidVolumn = tick.bidVolumn;
            newGoodsTick.thedate = new Date();
            goodsService.addGoodsTick(goodsDTO.id, newGoodsTick);

            sum_max_spread += tick.ask - tick.bid;
            double avg_spread = sum_max_spread / (count_size+1);
            double new_avg_spread = MathsUtils.round(avg_spread,6);
            goodsService.updateGoods(goodsDTO.id,goodsDTO.type,new_avg_spread);
        }

    }
}
